package com.example.chessbattleplatform.utils;

import com.example.chessbattleplatform.utils.CacheDB.ManuallyCacheDBUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@RequiredArgsConstructor
public class UserLoginHistoryIsCachedUtils {
    private final ManuallyCacheDBUtils manuallyCacheDBUtils;

    //缓存用户登录记录
    public void userLoginHistoryIsCached(Long userId, String token) {
        //将用户数据放入缓存
        manuallyCacheDBUtils.putJsonData("user_Id:" + userId, Map.of("jwt", token, "uid", userId), "UserLoginHistory");
    }
    //获取用户登录记录
    public String getUserLoginHistory(String userId) {
        //从缓存中获取用户数据
        Map<String, String> userLoginHistory = manuallyCacheDBUtils.getRocksDBToBean("user_Id:" + userId, Map.class, "UserLoginHistory");
        if (userLoginHistory == null){
            return null;
        }
        return userLoginHistory.get("jwt");
    }
    //删除用户登录记录
    public void deleteUserLoginHistory(Long userId) {
        //从缓存中删除用户数据
        manuallyCacheDBUtils.delete("user_Id:" + userId, "UserLoginHistory");
    }


}
